#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using Microsoft.WindowsMobile.Status;
using Microsoft.WindowsMobile.Forms;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using System.IO;
using System.Text.RegularExpressions;

#endregion
/** 
COPYRIGHT (C) 2008 Piyusha Desai. All Rights Reserved.
* Read Licence.txt before using this application.
*/
namespace SmsBuddy
{   
    public class MainForm : System.Windows.Forms.Form
    {

        private MenuItem menuDone;
        private MenuItem mainMenu;
        private MenuItem menuNewGroup;
        private MenuItem menuDeleteGroup;
       
        private System.Windows.Forms.MainMenu mainMenu1;

        private ImageList imgTreeView;

        SystemState missedCall = null;
        private ComboBox GroupList;
        private ComboBox optionEnabled;

        private TextBox txtMessage;
        private CheckBox sendMessage;
        private CheckBox sendSmily;
        private Label senderName;

        Buddy buddy = Buddy.Load();

        Group currentGroup = null; 

        private MenuItem menuEditGroup;
        private MenuItem menuAddContact;
        private PictureBox imgGroupIcon;
        private MenuItem menuItem3;
        private MenuItem menuItem1;
        private MenuItem menuAdvancedGroupOptions;
        private MenuItem menuSetGroupIcon;
        private MenuItem menuRemoveGroupIcon;
        private MenuItem menuItem4;
        private MenuItem menuMessageOptions;
        private MenuItem menuMissCall;
        private TextBox txtBill;
        private MenuItem menuShowContacts;
        private MenuItem menuMessageCounter;
        private MenuItem menuTextMessage;
        private MenuItem menuPictureMessage;



        public MainForm()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.menuDone = new System.Windows.Forms.MenuItem();
            this.mainMenu = new System.Windows.Forms.MenuItem();
            this.menuNewGroup = new System.Windows.Forms.MenuItem();
            this.menuEditGroup = new System.Windows.Forms.MenuItem();
            this.menuDeleteGroup = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.menuAddContact = new System.Windows.Forms.MenuItem();
            this.menuShowContacts = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuMessageOptions = new System.Windows.Forms.MenuItem();
            this.menuMessageCounter = new System.Windows.Forms.MenuItem();
            this.menuTextMessage = new System.Windows.Forms.MenuItem();
            this.menuPictureMessage = new System.Windows.Forms.MenuItem();
            this.menuAdvancedGroupOptions = new System.Windows.Forms.MenuItem();
            this.menuSetGroupIcon = new System.Windows.Forms.MenuItem();
            this.menuRemoveGroupIcon = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.menuMissCall = new System.Windows.Forms.MenuItem();
            this.imgTreeView = new System.Windows.Forms.ImageList();
            this.GroupList = new System.Windows.Forms.ComboBox();
            this.optionEnabled = new System.Windows.Forms.ComboBox();
            this.txtMessage = new System.Windows.Forms.TextBox();
            this.sendMessage = new System.Windows.Forms.CheckBox();
            this.sendSmily = new System.Windows.Forms.CheckBox();
            this.senderName = new System.Windows.Forms.Label();
            this.imgGroupIcon = new System.Windows.Forms.PictureBox();
            this.txtBill = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.menuDone);
            this.mainMenu1.MenuItems.Add(this.mainMenu);
            // 
            // menuDone
            // 
            this.menuDone.Text = "Done";
            this.menuDone.Click += new System.EventHandler(this.mnuDone_Click);
            // 
            // mainMenu
            // 
            this.mainMenu.MenuItems.Add(this.menuNewGroup);
            this.mainMenu.MenuItems.Add(this.menuEditGroup);
            this.mainMenu.MenuItems.Add(this.menuDeleteGroup);
            this.mainMenu.MenuItems.Add(this.menuItem3);
            this.mainMenu.MenuItems.Add(this.menuAddContact);
            this.mainMenu.MenuItems.Add(this.menuShowContacts);
            this.mainMenu.MenuItems.Add(this.menuItem1);
            this.mainMenu.MenuItems.Add(this.menuMessageOptions);
            this.mainMenu.MenuItems.Add(this.menuAdvancedGroupOptions);
            this.mainMenu.MenuItems.Add(this.menuMissCall);
            this.mainMenu.Text = "Menu";
            this.mainMenu.Click += new System.EventHandler(this.mainMenu_Click);
            // 
            // menuNewGroup
            // 
            this.menuNewGroup.Text = "New Group";
            this.menuNewGroup.Click += new System.EventHandler(this.mnuNewGroup_Click);
            // 
            // menuEditGroup
            // 
            this.menuEditGroup.Text = "Edit Group Name";
            this.menuEditGroup.Click += new System.EventHandler(this.menuEditName_Click);
            // 
            // menuDeleteGroup
            // 
            this.menuDeleteGroup.Text = "Delete Group";
            this.menuDeleteGroup.Click += new System.EventHandler(this.menuDeleteGroup_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Text = "-";
            // 
            // menuAddContact
            // 
            this.menuAddContact.Text = "Add Contact";
            this.menuAddContact.Click += new System.EventHandler(this.menuAddContact_Click);
            // 
            // menuShowContacts
            // 
            this.menuShowContacts.Text = "Show Contacts";
            this.menuShowContacts.Click += new System.EventHandler(this.menuShowContacts_Click);
            // 
            // menuItem1
            // 
            this.menuItem1.Text = "-";
            // 
            // menuMessageOptions
            // 
            this.menuMessageOptions.MenuItems.Add(this.menuMessageCounter);
            this.menuMessageOptions.MenuItems.Add(this.menuTextMessage);
            this.menuMessageOptions.MenuItems.Add(this.menuPictureMessage);
            this.menuMessageOptions.Text = " Message Options";
            // 
            // menuMessageCounter
            // 
            this.menuMessageCounter.Text = "Message Counter";
            this.menuMessageCounter.Click += new System.EventHandler(this.menuMessageCounter_Click);
            // 
            // menuTextMessage
            // 
            this.menuTextMessage.Text = "Text Message";
            this.menuTextMessage.Click += new System.EventHandler(this.menuTextMessage_Click);
            // 
            // menuPictureMessage
            // 
            this.menuPictureMessage.Text = "Picture Message";
            this.menuPictureMessage.Click += new System.EventHandler(this.menuPictureMessage_Click);
            // 
            // menuAdvancedGroupOptions
            // 
            this.menuAdvancedGroupOptions.MenuItems.Add(this.menuSetGroupIcon);
            this.menuAdvancedGroupOptions.MenuItems.Add(this.menuRemoveGroupIcon);
            this.menuAdvancedGroupOptions.MenuItems.Add(this.menuItem4);
            this.menuAdvancedGroupOptions.Text = "Advanced Group Options";
            // 
            // menuSetGroupIcon
            // 
            this.menuSetGroupIcon.Text = "Set Group Icon";
            this.menuSetGroupIcon.Click += new System.EventHandler(this.menuSetGroupIcon_Click);
            // 
            // menuRemoveGroupIcon
            // 
            this.menuRemoveGroupIcon.Text = "Remove Group Icon";
            this.menuRemoveGroupIcon.Click += new System.EventHandler(this.menuRemoveGroupIcon_Click);
            // 
            // menuItem4
            // 
            this.menuItem4.Text = "-";
            // 
            // menuMissCall
            // 
            this.menuMissCall.Text = "Miss Call View";
            this.menuMissCall.Click += new System.EventHandler(this.menuMissCall_Click);
            this.imgTreeView.Images.Clear();
            this.imgTreeView.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
            this.imgTreeView.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
            this.imgTreeView.Images.Add(((System.Drawing.Image)(resources.GetObject("resource2"))));
            // 
            // GroupList
            // 
            this.GroupList.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.GroupList.Location = new System.Drawing.Point(3, 62);
            this.GroupList.Name = "GroupList";
            this.GroupList.Size = new System.Drawing.Size(234, 24);
            this.GroupList.TabIndex = 0;
            this.GroupList.SelectedIndexChanged += new System.EventHandler(this.cmbGroupName_SelectedIndexChanged);
            // 
            // optionEnabled
            // 
            this.optionEnabled.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.optionEnabled.Items.Add("Disabled");
            this.optionEnabled.Items.Add("Enabled");
            this.optionEnabled.Location = new System.Drawing.Point(3, 86);
            this.optionEnabled.Name = "optionEnabled";
            this.optionEnabled.Size = new System.Drawing.Size(234, 24);
            this.optionEnabled.TabIndex = 1;
            // 
            // txtMessage
            // 
            this.txtMessage.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtMessage.Location = new System.Drawing.Point(4, 154);
            this.txtMessage.Multiline = true;
            this.txtMessage.Name = "txtMessage";
            this.txtMessage.Size = new System.Drawing.Size(231, 109);
            this.txtMessage.TabIndex = 4;
            // 
            // sendMessage
            // 
            this.sendMessage.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.sendMessage.Location = new System.Drawing.Point(4, 132);
            this.sendMessage.Name = "sendMessage";
            this.sendMessage.Size = new System.Drawing.Size(233, 22);
            this.sendMessage.TabIndex = 3;
            this.sendMessage.Text = "Send Message:";
            // 
            // sendSmily
            // 
            this.sendSmily.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.sendSmily.Location = new System.Drawing.Point(4, 110);
            this.sendSmily.Name = "sendSmily";
            this.sendSmily.Size = new System.Drawing.Size(233, 22);
            this.sendSmily.TabIndex = 2;
            this.sendSmily.Text = "Send Reminder";
            // 
            // senderName
            // 
            this.senderName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.senderName.Location = new System.Drawing.Point(4, 37);
            this.senderName.Name = "senderName";
            this.senderName.Size = new System.Drawing.Size(146, 22);
            this.senderName.Text = "Name:";
            // 
            // imgGroupIcon
            // 
            this.imgGroupIcon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.imgGroupIcon.Location = new System.Drawing.Point(185, 0);
            this.imgGroupIcon.Name = "imgGroupIcon";
            this.imgGroupIcon.Size = new System.Drawing.Size(50, 48);
            this.imgGroupIcon.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            // 
            // txtBill
            // 
            this.txtBill.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.txtBill.Location = new System.Drawing.Point(0, 0);
            this.txtBill.Name = "txtBill";
            this.txtBill.Size = new System.Drawing.Size(22, 30);
            this.txtBill.TabIndex = 7;
            this.txtBill.Text = "$$";
            // 
            // MainForm
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 266);
            this.Controls.Add(this.txtBill);
            this.Controls.Add(this.imgGroupIcon);
            this.Controls.Add(this.optionEnabled);
            this.Controls.Add(this.GroupList);
            this.Controls.Add(this.sendSmily);
            this.Controls.Add(this.sendMessage);
            this.Controls.Add(this.txtMessage);
            this.Controls.Add(this.senderName);
            this.KeyPreview = true;
            this.Menu = this.mainMenu1;
            this.Name = "MainForm";
            this.Text = "SMSBUDDY";
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.ResumeLayout(false);

        }

        #endregion

     
        // wait for sms notification
        static System.Threading.AutoResetEvent waitForSmsNotification = new System.Threading.AutoResetEvent(false);

       //Main with 3 condition
        static void Main(string[] args)
        {
            if (args.Length == 1 && args[0]== "-notification")
            {
               // if miss call go to miss call
                Application.Run(new MissCall());
            }
            else if (args.Length == 1 && args[0] == "-remoteAdmin")
            {
               //else for temp testing
            }                
            else if (args.Length == 1 &&
                      args[0] == "-textForward")
            {
                // Text Message Forwarding
                using (MessageInterceptor textForwardingInterceptor =
                    new MessageInterceptor(Buddy.TextForwardingName, false))
                {
                    textForwardingInterceptor.MessageReceived += new MessageInterceptorEventHandler(textForwardingInterceptor_MessageReceived);

                    waitForSmsNotification.WaitOne(30000, false);
                }
            }
            else
            {
               // or application run 
                Application.Run(new MainForm());
            }
        }
        //forwarding message 
        static void textForwardingInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            
            if (e.Message is SmsMessage)
            {
                SmsMessage sms = (SmsMessage)e.Message;

                ForwardTextMessage(sms);
            }
                             
            waitForSmsNotification.Set();
        }

        // forward the text message to particular contact
        private static void ForwardTextMessage(SmsMessage sms)
        {
            using (Buddy buddy = Buddy.Load())
            {
                if (buddy.TextMessageOptions.ForwardingEnabled)
                {
                    string body = sms.Body;

                    if (buddy.TextMessageOptions.AppendSenderInformation)
                    {
                        body += "\n((" + sms.From.Address + "))";
                    }

                    SmsMessage fwd = new SmsMessage(buddy.TextMessageOptions.ForwardNumber, body);
                    try
                    {
                        SendSms(fwd);
                    }
                    catch
                    {
                        
                    }
                }
            }
        }
        //if message receive ready to send
        static void interceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            if(e.Message is SmsMessage)
            {              
                SmsMessage sms = (SmsMessage)e.Message;

                PerformRemoteRequest(sms);
            }
             waitForSmsNotification.Set();
        }

        //call other two function here        
        private static void PerformRemoteRequest(SmsMessage sms)
        {
            RemoteAdministration(sms);
            ParseForRemoteQuery(sms);
        }
             
        //check here   
        private static void ParseForRemoteQuery(SmsMessage sms)
        {
            string[] tokens;
            if (GetCommand(sms.Body, 1, out tokens))
            {
                switch (tokens[0].ToUpper())
                {
                    
                    case "NEXTREMINDER":
                        SendNextReminder(sms.From.Address);
                        break;
                    case "CURREMINDER":

                        SendCurReminder(sms.From.Address);
                        break;
                }
            }
            
        }
        // send current set message from your reminder
        private static void SendCurReminder(string address)
        {
            SendReminder(address, SystemState.CalendarAppointment);
        }

        //  send next message from your reminder
        private static void SendNextReminder(string address)
        {
            SendReminder(address, SystemState.CalendarNextAppointment);
        }

             
       // here it is actual sending 
        private static void SendReminder(string address, Appointment appointment)
        {
            string message = "";
            if (appointment != null)
            {
                 message = appointment.Subject + "\r";
                if (appointment.Location.Length > 0)
                {
                    message += appointment.Location + "\r";
                }

                message += appointment.Start.ToString() + " - " + appointment.End.ToString();
            }
            else
            {
                message = "Nothing set to send here.";
            }
            
           //system class used here
            SmsMessage sms = new SmsMessage(address, message);
            SendSms(sms);
        }

        //buddy object            
        static void SendSms(SmsMessage sms)
        {
            Buddy buddy = Buddy.Load();
            if (buddy != null)
            {
                sms.Send();
            }
        }

      
        private static bool GetCommand(string text, int commandTokenCount, out string[] commandTokens)
        {
            string[] allTokens = text.Split(new char[] { '.' });
            commandTokens = null;
            
         
            if (commandTokenCount + 1 <= allTokens.Length)
            {
                ArrayList list = new ArrayList();
                for (int index = allTokens.Length - commandTokenCount - 1;
                    index < allTokens.Length - 1;
                    index++)
                {
                    list.Add(allTokens[index]);
                }

                if (list.Count > 0)
                {
                    commandTokens = (string[])list.ToArray(typeof(string));
                }
            }

            return commandTokens != null;
        }

      
        private static void RemoteAdministration(SmsMessage sms)
        {
            string[] tokens;
            if (GetCommand(sms.Body, 3, out tokens))
            {
                switch (tokens[0].ToUpper())
                {
                    case "ENABLE":
                        // Set the enabled bit of a group
                        EnableGroup(sms.From.Address, tokens[1], tokens[2]);
                        break;
                }
            }

            if (GetCommand(sms.Body, 1, out tokens))
            {
                switch (tokens[0].ToUpper())
                {
                      case "LIST":
                        ListGroups(sms.From.Address);
                        break;
                }
            }
        }

        private static void EnableGroup(string smsAddress, string groupIdString, string enableString)
        {
            try
            {
                int id;
                if (groupIdString.ToUpper() == "ALL")
                {
                    id = -1;
                }
                else
                {
                    id = int.Parse(groupIdString);
                }

               
                bool enable = enableString == "1";

                using (Buddy buddy = new Buddy())
                {
                 
                    if (id < buddy.Groups.Count && id >= 0)
                    {
                                      
                        buddy.Groups[id].Enabled = enable;
                        buddy.Save();

                     
                        string message = "Group " + id + ": '" + buddy.Groups[id].Name + "' was set to ";
                        message += enable ? "Enabled" : "Disabled";

                  
                        SmsMessage sms = new SmsMessage(smsAddress, message);
                        SendSms(sms);
                    }
                    else if (id == -1)
                    {
                        
                        foreach (Group group in buddy.Groups)
                        {
                            group.Enabled = enable;
                        }

                        buddy.Save();

                        string message = "All groups are now ";
                        message += enable ? "Enabled" : "Disabled";

                        SmsMessage sms = new SmsMessage(smsAddress, message);
                        SendSms(sms);
                    }
                }
            }
            catch
            {
                SmsMessage sms = new SmsMessage(smsAddress, "An error occurred");
                SendSms(sms);
            }
        }

  
        // List of all groups with their status either enabled or disabled
          
        private static void ListGroups(string smsAddress)
        {
            using (Buddy buddy = Buddy.Load())
            {
                string message = "Groups:\r";
                foreach (Group group in buddy.Groups)
                {
                    message += group.Id + ":" + group.Name + " (" +
                        (group.Enabled ? "Enabled" : "Disabled") + ")\r";
                }

                SmsMessage sms = new SmsMessage(smsAddress, message);
                SendSms(sms);
            }
        }

        
        private void mnuDone_Click(object sender, EventArgs e)
        {
            CommitData();

            SetNotifications();

            buddy.Dispose();

            Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            buddy.CheckCustomProperty();

            CheckForGroups();

            RefreshGroups();

            GroupList.Focus();

        }

        private void RefreshGroups()
        {
            GroupList.Items.Clear();

            foreach (Group group in buddy.Groups)
            {
                AddGroup(group);
            }

            GroupList.SelectedIndex = 0;
        }

        private void AddGroup(Group group)
        {
            GroupList.Items.Add(group);
        }

    
        private void CheckForGroups()
        {
            if (buddy.Groups.Count == 0)
            {
                Group group = new Group();

                group.Message = "Sorry, I am unable to answer your phone right now, I will call you back when I am free.";
                group.SendMeetingInfo = false;
                group.SendMessage = true;
                group.Name = "<Default>";
                group.Enabled = false;
                buddy.Groups.Add(group);

                group = new Group();
                group.Message = "Sorry,I am unable to answer your phone right now, will call you back when I am free .";
                group.SendMeetingInfo = false;
                group.SendMessage = true;
                group.Name = "<Unknown Caller>";
                group.Enabled = false;
                buddy.Groups.Add(group);

                CommitData();
      
            }
        }
        
            
        private void mnuNewGroup_Click(object sender, EventArgs e)
        {
            QuestionDialog name = new QuestionDialog();
            name.Question = "Enter a group name Here :";
            if (name.ShowDialog() == DialogResult.OK)
            {
                Group group = new Group();
                group.Name = name.Answer;
                group.Enabled = false;
                buddy.Groups.Add(group);
                CommitData();
                AddGroup(group);
                GroupList.SelectedItem = group;
            }
        }

        
        private void CommitData()
        {
            StoreCurrentGroupData();

            buddy.Save();
        }

        #region State and Notification Broker: Application Launcher
        /// <summary>
        /// Determines if any of the groups are enabled.  If at least one group is
        /// enabled, then we enable the State and Notification Broker's Application
        /// Launcher to launch the app when a call is missed.
        /// </summary>
        private void SetNotifications()
        {
            if (buddy.AreAnyEnabled())
            {
                // at least one of the groups are enabled.  Let's set up the 
                // Application Launcher


                // we are interested when the missed call count changes and is 
                // greater than 0. We use the 'missedCall' SystemState instance
                // and the PhoneMissedCalls SystemProperty
                missedCall = new SystemState(SystemProperty.PhoneMissedCalls);

                // A change that is greater than 0 means that we missed a call.
                // A change that IS 0 means that the count was reset.
                missedCall.ComparisonType = StatusComparisonType.Greater;
                missedCall.ComparisonValue = 0;

                // because we want to specify and argument to our existing application,
                // we need to get the full path the application.
                string path = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;

                // Enable the application launcher to call this app with '-notification' on the command line
                missedCall.EnableApplicationLauncher(
                    Buddy.MissedCallIdName, 
                    path, 
                    "-notification");
            }
            else
            {
                // none of the groups are enabled, so disable the Application Launcher
                if (SystemState.IsApplicationLauncherEnabled(Buddy.MissedCallIdName))
                {
                    SystemState.DisableApplicationLauncher(Buddy.MissedCallIdName);
                }
            }
        }

        #endregion


        private void cmbGroupName_SelectedIndexChanged(object sender, EventArgs e)
        {
            CommitData();

            currentGroup = (Group)GroupList.SelectedItem;

            ViewCurrentGroupData();

            EnableMenus();
        }

        private void EnableMenus()
        {
           
            if (GroupList.SelectedIndex <= 1)
            {
                menuDeleteGroup.Enabled = false;
                menuAddContact.Enabled = false;
                menuShowContacts.Enabled = false;
                
            }
            else
            {
                menuDeleteGroup.Enabled = true;
                menuAddContact.Enabled = true;
                menuShowContacts.Enabled = true;
               
            }

        }
                   
        private void ViewCurrentGroupData()
        {
            optionEnabled.SelectedIndex = currentGroup.Enabled ? 1 : 0;
            sendSmily.Checked = currentGroup.SendMeetingInfo;
            sendMessage.Checked = currentGroup.SendMessage;
            txtMessage.Text = currentGroup.Message;

            if (currentGroup.Image.Length > 0)
            {
                imgGroupIcon.Image = new Bitmap(currentGroup.Image);
            }
            else
            {
                imgGroupIcon.Image = null;
            }
        }

   
      
        private void StoreCurrentGroupData()
        {
            if (currentGroup != null)
            {
                currentGroup.Enabled = optionEnabled.SelectedIndex == 1;
                currentGroup.SendMessage = sendMessage.Checked;
                currentGroup.SendMeetingInfo = sendSmily.Checked;
                currentGroup.Message = txtMessage.Text;
            }
        }

               
        private void menuEditName_Click(object sender, EventArgs e)
        {
            QuestionDialog name = new QuestionDialog();
            name.Question = "Enter a name for your group:";
            name.Answer = currentGroup.Name;

            if (name.ShowDialog() == DialogResult.OK)
            {
                currentGroup.Name = name.Answer;
                CommitData();
                RefreshGroups();
                GroupList.SelectedItem = currentGroup;
            }
        }

        private void menuDeleteGroup_Click(object sender, EventArgs e)
        {
            if (currentGroup != null)
            {
                if (MessageBox.Show("Are you sure you want to remove this group '" + currentGroup.Name + "'?",
                    "Confirm Remove",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    currentGroup.RemoveAllContacts();

                    buddy.Groups.Remove((Group)GroupList.SelectedItem);
                    CommitData();
                    RefreshGroups();
                }
            }
        }

        private void menuShowContacts_Click(object sender, EventArgs e)
        {
            ContactInfo frmContacts = new ContactInfo(buddy, currentGroup);

            frmContacts.ShowDialog();
        }

        private void menuAddContact_Click(object sender, EventArgs e)
        {
            if (currentGroup != null)
            {
                currentGroup.AddContact();
            }
            Group g = new Group();
            ContactInfo c = new ContactInfo(buddy,g);
            c.Show();
        }

        

        private void menuSetGroupIcon_Click(object sender, EventArgs e)
        {
            if (currentGroup != null)
            {
              
                SelectPictureDialog picDlg = new SelectPictureDialog();

       
                picDlg.CameraAccess = true;
                
                if (picDlg.ShowDialog() == DialogResult.OK)
                {
                    currentGroup.Image = picDlg.FileName;
                    imgGroupIcon.Image = new Bitmap(picDlg.FileName);
                }
            }
        }

        private void menuRemoveGroupIcon_Click(object sender, EventArgs e)
        {
            if (currentGroup != null)
            {
              
                currentGroup.Image = "";
                imgGroupIcon.Image = null;
            }
        }

        private void menuTextMessageOptions_Click(object sender, EventArgs e)
        {
         
            SmsOptions dlg = new SmsOptions(buddy);

            if (dlg.ShowDialog() == DialogResult.OK)
            {
      
                buddy.Save();

             
                if (buddy.TextMessageOptions.ForwardingEnabled &&
                    buddy.TextMessageOptions.ForwardNumber != null &&
                    buddy.TextMessageOptions.ForwardNumber.Length > 0)
                {
                    MessageInterceptor mi;
                    if (MessageInterceptor.IsApplicationLauncherEnabled(Buddy.TextForwardingName))
                    {
                        mi = new MessageInterceptor(Buddy.TextForwardingName);
                    }
                    else
                    {
                        mi = new MessageInterceptor();
                    }

                    if (buddy.TextMessageOptions.DeleteLocalMessage)
                    {
                        mi.InterceptionAction = InterceptionAction.NotifyAndDelete;
                    }
                    else
                    {
                        mi.InterceptionAction = InterceptionAction.Notify;
                    }

                    mi.EnableApplicationLauncher(
                        Buddy.TextForwardingName,
                        System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName,
                        "-textForward");

                    mi.Dispose();
                }
                else if (!buddy.TextMessageOptions.ForwardingEnabled)
                {
                    if (MessageInterceptor.IsApplicationLauncherEnabled(Buddy.TextForwardingName))
                    {
                        MessageInterceptor mi = new MessageInterceptor(Buddy.TextForwardingName);
                        mi.DisableApplicationLauncher();
                        mi.Dispose();
                    }
                }
            }
        }                                      

        private void menuMissCall_Click(object sender, EventArgs e)
        {
            MissCall m = new MissCall();
            m.Show();
        }     
        private void menuTextMessage_Click(object sender, EventArgs e)
        {
            SmsOptions s = new SmsOptions(buddy);
            s.Show();
        }
        private void menuMessageCounter_Click(object sender, EventArgs e)
        {
            MessageCounter mc = new MessageCounter();
            mc.Show();
        }
        private void menuPictureMessage_Click(object sender, EventArgs e)
        {
            PictureMessageOptions p = new PictureMessageOptions();
            p.Show();
        }

        private void mainMenu_Click(object sender, EventArgs e)
        {

        }

       

             

    

       

    }
}

